/**
 * 相册服务 - 封装相册相关的IPC调用
 */

import { Album, AlbumListResponse, AlbumCreateParams, AlbumUpdateParams } from '../types'

/**
 * 相册服务类
 */
export class AlbumService {
  /**
   * 获取相册列表
   * @param params 查询参数
   * @returns 相册列表响应
   */
  static async getList(params?: {
    page?: number
    limit?: number
    orderBy?: string
    orderDirection?: 'ASC' | 'DESC'
  }): Promise<AlbumListResponse> {
    const response = await window.electronAPI.albums.getList(params || {})
    
    if (!response.success) {
      throw new Error(response.error || '获取相册列表失败')
    }
    
    return response.data
  }

  /**
   * 获取相册详情
   * @param id 相册ID
   * @returns 相册详情
   */
  static async getById(id: number): Promise<Album & { photos: any[] }> {
    const response = await window.electronAPI.albums.getById({ id })
    
    if (!response.success) {
      throw new Error(response.error || '获取相册详情失败')
    }
    
    return response.data
  }

  /**
   * 创建相册
   * @param data 相册数据
   * @returns 创建的相册信息
   */
  static async create(data: AlbumCreateParams): Promise<Album> {
    const response = await window.electronAPI.albums.create(data)
    
    if (!response.success) {
      throw new Error(response.error || '创建相册失败')
    }
    
    return response.data
  }

  /**
   * 更新相册信息
   * @param id 相册ID
   * @param updates 更新数据
   * @returns 更新后的相册信息
   */
  static async update(id: number, updates: AlbumUpdateParams): Promise<Album> {
    const response = await window.electronAPI.albums.update({ id, updates })
    
    if (!response.success) {
      throw new Error(response.error || '更新相册失败')
    }
    
    return response.data
  }

  /**
   * 删除相册
   * @param id 相册ID
   * @returns 删除结果
   */
  static async delete(id: number): Promise<{ id: number }> {
    const response = await window.electronAPI.albums.delete({ id })
    
    if (!response.success) {
      throw new Error(response.error || '删除相册失败')
    }
    
    return response.data
  }

  /**
   * 向相册添加照片
   * @param albumId 相册ID
   * @param photoId 照片ID
   * @returns 操作结果
   */
  static async addPhoto(albumId: number, photoId: number): Promise<any> {
    const response = await window.electronAPI.albums.addPhoto({ albumId, photoId })
    
    if (!response.success) {
      throw new Error(response.error || '添加照片到相册失败')
    }
    
    return response.data
  }

  /**
   * 从相册移除照片
   * @param albumId 相册ID
   * @param photoId 照片ID
   * @returns 操作结果
   */
  static async removePhoto(albumId: number, photoId: number): Promise<any> {
    const response = await window.electronAPI.albums.removePhoto({ albumId, photoId })
    
    if (!response.success) {
      throw new Error(response.error || '从相册移除照片失败')
    }
    
    return response.data
  }
}